import axios from 'axios'
const instance = axios.create({
  baseURL: '/api'
})
instance.interceptors.request.use(config => {
  const _obj = {
    r: Math.random()
  }
  if (!config.params) {
    config.params = _obj
  } else {
    config.params = Object.assign(config.params, _obj)
  }
  return config
})
instance.interceptors.response.use(response => {
  console.log(response)
  if (response.status === 200) {
    return response.data
  } else {
    return Promise.reject(response.data)
  }
}, error => {
  const err = error.response
  if (err.status === 401) {
    location.href = '/login'
  }
  return Promise.reject(err)
})
export default instance
